﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using System.Windows.Forms;

namespace SPMigrationOracleUtility
{
    class Collect_Site
    {
        string mainpath = "";
        ComboBox ocmbbx;
        ListBox olstbx;
        string siteUrl = "";

        public void GetSubSites(ComboBox cmbbox, string url)
        {
            mainpath = url;
            try
            {
                this.ocmbbx = cmbbox;
                List<ListItemPair> data = new List<ListItemPair>();
                using (ClientContext cliContext = new ClientContext(mainpath))
                {
                    Web web = cliContext.Web;
                    cliContext.Load(web);
                    cliContext.ExecuteQuery();
                    ocmbbx.Items.Add(new ListItemPair() { Value = Convert.ToString(web.Id), Text = web.Title, URL = url });
                }
                ocmbbx.DisplayMember = "Text";
                getSubWebs(mainpath);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void getSubWebs(string path)
        {
            try
            {
                ClientContext clientContext = new ClientContext(path);
                Web oWebsite = clientContext.Web;
                clientContext.Load(oWebsite, website => website.Webs, website => website.Title);
                clientContext.ExecuteQuery();

                foreach (Web orWebsite in oWebsite.Webs)
                {
                    string newpath = mainpath + orWebsite.ServerRelativeUrl;
                    ocmbbx.Items.Add(new ListItemPair() { Value = Convert.ToString(orWebsite.Id), Text = orWebsite.Title, URL = newpath });
                    getSubWebs(newpath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void GetLists(ListBox lstBox, ComboBox lstSubSite, string url)
        {
            mainpath = url;
            try
            {
                this.olstbx = lstBox;
                olstbx.Items.Clear();
                ListItemPair subsite = (ListItemPair)lstSubSite.SelectedItem;
                List<ListItemPair> data = new List<ListItemPair>();
                using (ClientContext clientContext = new ClientContext(mainpath))
                {
                    Web site = clientContext.Web;
                    clientContext.Load(site);
                    clientContext.ExecuteQuery();
                    if (subsite.Text == site.Title)
                    {
                        Web oweb = clientContext.Web;
                        ListCollection listColl = oweb.Lists;
                        clientContext.Load(listColl);
                        clientContext.ExecuteQuery();
                        foreach (List olist in listColl)
                        {
                            olstbx.Items.Add(new ListItemPair() { Value = Convert.ToString(olist.Id), Text = olist.Title, ListTemplate = olist.BaseType, URL = subsite.URL });
                        }
                    }
                }
                olstbx.DisplayMember = "Text";
                url = siteUrl;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public Web GetWebByTitle(ClientContext clientContext, Web rootWeb, string siteTitle)
        {
            try
            {
                var query = clientContext.LoadQuery(
                rootWeb.Webs.Where(p => p.Title == siteTitle));
                clientContext.ExecuteQuery();
                return query.FirstOrDefault();
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void GetListFields(ComboBox cmbList, ComboBox cmbListFields, string url)
        {
            cmbListFields.Items.Clear();
            cmbListFields.DisplayMember = "displayName";
            try
            {
                using (ClientContext clientContext = new ClientContext(url))
                {
                    Web oweb = clientContext.Web;
                    ListItemPair listName = (ListItemPair)cmbList.SelectedItem;
                    List lst = oweb.Lists.GetByTitle(listName.Text);
                    FieldCollection fldCollec = lst.Fields;
                    clientContext.Load(lst);
                    clientContext.Load(fldCollec);
                    clientContext.ExecuteQuery();
                    foreach (Field fld in fldCollec)
                    {
                        if (fld.InternalName == "Title" || fld.InternalName == "FileLeafRef")
                        {
                            if(lst.BaseType  ==BaseType.DocumentLibrary &&  fld.InternalName == "FileLeafRef")
                            cmbListFields.Items.Add(new DestFieldType { displayName = fld.Title, internalName = fld.InternalName, fieldType = fld.TypeAsString });
                            else if(fld.InternalName == "Title" )
                                cmbListFields.Items.Add(new DestFieldType { displayName = fld.Title, internalName = fld.InternalName, fieldType = fld.TypeAsString });
                            
                        }
                        else if (!fld.ReadOnlyField && !fld.Hidden)
                        {
                            if (!fld.FromBaseType)
                                cmbListFields.Items.Add(new DestFieldType { displayName = fld.Title, internalName = fld.InternalName, fieldType = fld.TypeAsString });
                        }

                    }
                    if (cmbListFields.Items.Count > 0)
                        cmbListFields.SelectedIndex = 0;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Invalid URL");
            }
        }

        public void GetListFieldType(ComboBox cmbListFields, Label FieldType)
        {
            DestFieldType objfld = (DestFieldType)cmbListFields.SelectedItem;
            FieldType.Text = objfld.fieldType;
        }

    }
}
